﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Mac.Foundation;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace CPFUI.IControl
{
    [CPF.Design.DesignerLoadStyle("res://$safeprojectname$/Stylesheet1.css")]//用于设计的时候加载样式
    public class TabBarControl : Control
    {

        public TabBarControl()
        {
            Items = new Collection<TabBarItem>();
        }
        [Description("是否可移动标签"), DefaultValue(true)]
        public bool ChangedMove
        {
            get { return GetValue<bool>(); }
            set { SetValue(value); }
        }
        public float ItemHeight
        {
            get { return GetValue<float>(); }
            set { SetValue(value); }
        }
        public float ItemWidth
        {
            get { return GetValue<float>(); }
            set { SetValue(value); }
        }
        /// <summary>
        /// 布局方向
        /// </summary>
        [Description("布局方向"), DefaultValue(Orientation.Vertical)]
        public Orientation Orientation
        {
            get { return GetValue<Orientation>(); }
            set
            {
                SetValue(value);

                SetPosition();
                //Invalidate();
            }
        }
        /// <summary>
        /// 获取新下标为index之前的所有项的高度
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public float GetHeight(int index)
        {
            float height = 0;
            for (int i = 0; i < Items.Count; i++)
            {
                if (Items[i].NewTabPageIndex < index)
                {
                    height += Items[i].Height.Value;
                }
            }
            return height;
        }
        /// <summary>
        /// 获取新下标为index之前的所有项的宽度
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public float GetWidth(int index)
        {
            float width = 0;
            for (int i = 0; i < Items.Count; i++)
            {
                if (Items[i].NewTabPageIndex < index)
                {
                    width += Items[i].Width.Value;
                }
            }
            return width;
        }
        public Collection<TabBarItem> Items
        { 
            get { return GetValue<Collection<TabBarItem>>(); }
            set { SetValue(value); }
        }
        //模板定义
        protected override void InitializeComponent()
        {
        }
        public void AddTabBarItem(TabBarItem item)
        {
            Items.Add(item);
            Children.Add(item);
            item.MarginLeft = 0;
            item.MarginTop= 0;
            item.HostTabBarControl = this;
            item.NewTabPageIndex = Items.Count - 1;

            SetPosition();
        }
        public void SetPosition()
        {
            if (Orientation == Orientation.Vertical)
            {
                float top = 0;
                for (int i = 0; i < Items.Count; i++)
                {
                    Items[i].Width = this.Width;
                    Items[i].Height = ItemHeight;
                    Items[i].MarginLeft = 0;
                    Items[i].MarginTop = top;
                    top += Items[i].Height.Value;
                }
            }
            else if (Orientation == Orientation.Horizontal)
            {
                float left = 0;
                for (int i = 0; i < Items.Count; i++)
                {
                    Items[i].Width = ItemWidth;
                    Items[i].Height = this.Height;
                    Items[i].MarginLeft = left;
                    Items[i].MarginTop = 0;
                    left += Items[i].Width.Value;
                }
            }
        }
        /// <summary>
        /// 查找当前移动标签所在的标签
        /// </summary>
        /// <param name="item">移动标签</param>
        /// <returns>目标标签</returns>
        public TabBarItem FindMoveItemToItem(TabBarItem item)
        {
            Rect r =new Rect(item.MarginLeft.Value,item.MarginTop.Value,item.Width.Value,item.Height.Value);
            Point p=new Point(r.X+ item.Width.Value/2, r.Y+item.Height.Value/2);
            for (int i = 0; i < Items.Count; i++)
            {
                if (item == Items[i])
                {
                    continue;
                }
                Rect temp = new Rect(Items[i].MarginLeft.Value, Items[i].MarginTop.Value, Items[i].Width.Value, Items[i].Height.Value);
                if (IsPointInRectangle(p, temp))
                {
                    return Items[i];
                }
            }
            return null;
        }
        public bool IsPointInRectangle(Point p, Rect rect)
        {
            return (p.X >= rect.Left && p.X <= rect.Right && p.Y >= rect.Top && p.Y <= rect.Bottom);
        }
#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错
        protected override void OnInitialized()
        {
            base.OnInitialized();

        }
        //用户代码

#endif
    }
}
